<?php
$version = Zend_Registry::get('version');
$this->letterForm->setEditor($this->serverUrl().$this->baseUrl());
$this->letterForm->getEditor();
?>

<script type="text/javascript">
$(document).ready(function() {	
	floatingbar('#floatingbar',"#button_top_bar",null);
	function openMsgDialog(msg)
	{
		$('#dialog_msg').html(msg).dialog({
			autoOpen: true,
			modal: true,
			show: 'fade',
			resizable: 'false',				
			buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
			open: function(event, ui)
				 {
					setTimeout(function() 
					{									
						if($('#' + event.target.id).dialog('isOpen'))
						{
							$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
						}
					}, 10000);
				  }
		});
	}
	
	$('#email_name').change(function() {	
		var sel = $('#email_name option:selected').val();	
		if(sel == 'all')
		{
			$('#email_name option:selected').attr( 'selected' ,'');			
			$('#email_name option:first-child').attr( 'selected', 'selected');
		}
	});
		
	$('a.reset_btn').click(function() {			
		 resetFields();	
		 removeError();	
	});
	
	function resetFields()
	{
		$( "form" )[ 0 ].reset();				
		if(tinyMCE.get('templates_desc'))
		{
			tinyMCE.get('templates_desc').setContent('');
		}
	}
	
	function removeError()
	{
		$('#subject_err').html('');
		$('#subject').removeClass('ui-state-error');
		$('#email_name_err').html('');
		$('#email_name').removeClass('ui-state-error');		
		$('#templates_desc_err').html('');
		$('#templates_desc').removeClass('ui-state-error');		
		
		$('#actionMessage').html('');
	}
	
	function addError(key,errMsg)
	{
		var err_id = key+'_err';
		var field_name = key;
		$('#'+field_name).addClass('ui-state-error');
		$('#'+err_id).append(errMsg);
	}
	
	function openLoaderDialog()
	{
		$('#dialog_container').dialog({
					autoOpen: true,
					modal: true,
					show: 'fade',
					resizable: 'false',				
					open: function(event, ui)
						 {
						 	$("#progressbar").progressbar({
								value:100
				
							});							
						  }
				});
	}
	
	function closeLoaderDialog()
	{
		$('#dialog_container').dialog('option','hide','explode').dialog('close');
	}
	
	$('a.send_template_btn').click(function() {			
			
			removeError();
			openLoaderDialog();
			var templates_desc_data = '';
			if(tinyMCE.get('templates_desc'))
			{
				templates_desc_data = tinyMCE.activeEditor.getContent();
			}
			else
			{
				templates_desc_data = $('#templates_desc').val();				
			}
			
		$.ajax({
			url: "<?php echo $this->url( array('module' => 'Newsletter','controller' => 'Templates', 'action'     => 'send' ), 'adminrout',    true) ?>",
			  	type: 'POST',
			  	data: { subject: $('#subject').val(), email_name: $('#email_name').val(), templates_desc: templates_desc_data },
				success: function(response){
					//alert(response);					
					var json_arr = eval("("+response+")");					
					closeLoaderDialog();					
					if(json_arr.status == 'ok')
					{
						$('#actionMessage').html(succMsgDesign(json_arr.msg));						
						resetFields();							
					}
					else if(json_arr.status == 'errV')
					{
						$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
												
						for(var i= 0; i < json_arr.msg.length; i++)
						{
							addError(json_arr.msg[i].key,json_arr.msg[i].value);							
						}
					}
					else
					{
						$('#actionMessage').html(errMsgDesign(json_arr.msg));
					}
				},
				error: function(xhr, status, error){
					closeLoaderDialog();
					var msg = "Error! " + xhr.status + " " + error;
					$('#actionMessage').html(errMsgDesign(msg));
				}
		});
	});
	
	$('#templates_name').change(function() {			
		if($('#templates_name').val() == '0')
		{
			if(tinyMCE.get('templates_desc'))
			{
				tinyMCE.get('templates_desc').setContent('');
			}
			else
			{
				$('#templates_desc').val('');				
			}
		}
		else
		{		
			$.ajax({
				url: "<?php echo $this->url( array('module' => 'Newsletter','controller' => 'Templates', 'action'     => 'template' ), 'adminrout',    true) ?>",
					type: 'POST',
					data: { id: $('#templates_name').val() },
					beforeSend: function(){
						$('#templates_name_loader').html('<img src="application/modules/Administrator/layouts/scripts/images/loader/ajax_loader.gif" border="0" height="16" class="left-float"/>');
					 },
					success: function(response){
						//alert(response);					
						var json_arr = eval("("+response+")");	
										
						$('#templates_name_err').html('');	
						$('#templates_name_loader').html('');	
								
						if(json_arr.status == 'ok')
						{
							if(tinyMCE.get('templates_desc'))
							{
								tinyMCE.get('templates_desc').setContent(json_arr.template['templates_desc']);
							}
							else
							{
								$('#templates_desc').val(json_arr.template['templates_desc']);				
							}						
						}
						else if(json_arr.status == 'errV')
						{
							$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
													
							for(var i= 0; i <= json_arr.msg.length; i++)
							{
								addError(json_arr.msg[i].key,json_arr.msg[i].value);							
							}
						}
						else
						{
							$('#actionMessage').html(errMsgDesign(json_arr.msg));
						}
					}
			});
		}
	});

	
});

</script>
<div id="container">
<?php if (!$this->allow()) : ?>
	<div class="grid_12 t_a_bottom">
		<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
	</div>
<?php else : ?>

<div class="title-container">
	<div class="newsletter-send"><h1><?php echo $this->translator->translator('newsletter_send_page_name'); ?></h1></div>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Newsletter','controller' => 'Templates', 'action'     => 'list' ), 'adminrout',    false) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="send_template_btn"><span class="icon save"><?php echo $this->translator->translator('newsletter_send_page_button'); ?></span></a></span>
    </div>
</div>
<div id="button_top_bar"></div>
<div class="content-container">
	<div id="actionMessage"></div>
   	
    <form name="formCategory" id="formCategory" method="post">
    	<div id="generalInfo">
        	<fieldset class="fieldset last">
                	<legend class="legend"><?php echo $this->translator->translator('newsletter_temp_add_page_general_info'); ?></legend>
                    <div class="form">
                    	<div class="form-row">
                        	<div class="label article-label">
                           		<?php echo $this->letterForm->subject->getLabel(); if($this->letterForm->subject->isRequired()){?><span class="required">*</span><?php } ?>
                          	</div>
                            
                          	<div class="field article-field">
                            	<?php  echo $this->letterForm->subject;  ?>
                                <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->letterForm->subject->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                <div class="input-errors" id="subject_err"></div>
                            </div>
                        </div>
                        
                        <div class="form-row">
                        	<div class="label article-label">
                           		<?php echo $this->letterForm->email_name->getLabel(); if($this->letterForm->email_name->isRequired()){?><span class="required">*</span><?php } ?>
                          	</div>
                            
                          	<div class="field article-field">
                            	<?php echo $this->letterForm->email_name; ?>
                                <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->letterForm->email_name->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                <div class="input-errors" id="email_name_err"></div>
                            </div>
                        </div>
                        
                        <div class="form-row">
                        	<div class="label article-label">
                           		<?php echo $this->letterForm->templates_name->getLabel(); if($this->letterForm->templates_name->isRequired()){?><span class="required">*</span><?php } ?>
                          	</div>
                            
                          	<div class="field article-field">
                            	<div class="left-float"><?php echo $this->letterForm->templates_name; ?></div><span id="templates_name_loader"></span>
                                <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->letterForm->templates_name->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                <div class="input-errors" id="templates_name_err"></div>
                            </div>
                        </div>
                        
                        <div class="form-row">
                        	<div class="label article-label">
                           		<?php echo $this->letterForm->templates_desc->getLabel(); if($this->letterForm->templates_desc->isRequired()){?><span class="required">*</span><?php } ?>
                          	</div>
                            
                          	<div class="field article-field">
                            	<div class="editor"><?php echo $this->letterForm->templates_desc; ?></div>
                                <div class="ui-widget ui-helper-clearfix info"><div class="ui-widget-header ui-corner-all" title="<?php echo $this->escape($this->letterForm->templates_desc->getAttrib('title')); ?>"><p><span class="ui-icon ui-icon-info"></span></p></div></div>
                                <span id="loaderLink"><a href="javascript:void(0);" onClick="loadTinyMCE('templates_desc');"><img src="application/modules/Administrator/layouts/scripts/images/common/editors.gif" border="0" title="<?php echo $this->translator->translator('common_editor_open'); ?>" alt="<?php echo $this->translator->translator('common_editor_open'); ?>" ></a></span>
                                <div class="input-errors" id="templates_desc_err"></div>
                            </div>
                        </div>
					</div>
   			</fieldset>
        </div>		  
	</form>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Newsletter','controller' => 'Templates', 'action'     => 'list' ), 'adminrout',    false) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="send_template_btn"><span class="icon save"><?php echo $this->translator->translator('newsletter_send_page_button'); ?></span></a></span>
    </div>
    
</div>

<div id="floatingbar">
	<h1><?php echo $this->translator->translator('newsletter_send_page_name'); ?></h1>
    
    <div class="button-container">
        <span class="button ui-widget-content-1"><a href="<?php echo $this->url( array('module' => 'Newsletter','controller' => 'Templates', 'action'     => 'list' ), 'adminrout',    false) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
        <span class="button ui-widget-content-1"><a href="javascript: void(0);" class="send_template_btn"><span class="icon save"><?php echo $this->translator->translator('newsletter_send_page_button'); ?></span></a></span>
    </div>
</div>
<?php endif; ?>

<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('newsletter_send_page_name')); ?>"></div> 
<div id="dialog_container" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>">
	<div id="progressbar" ></div>	
</div>
</div>